package org.ws.lifebacklog.activity;

import java.util.Stack;

import org.ws.lifebacklog.util.Callback;
import org.ws.lifebacklog.util.Logger;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;

public class GroupBaseActivity extends ActivityGroup {

	protected Stack<ActivityInfo> history = new Stack<GroupBaseActivity.ActivityInfo>();

	private static class ActivityInfo {
		private String id;
		private View view;
		private Integer requestCode;
		private Callback callback;

		public ActivityInfo(String id, View view, Callback callback) {
			super();
			this.id = id;
			this.view = view;
			this.callback = callback;
		}

		public ActivityInfo(String id, View view, int requestCode, Callback callback) {
			super();
			this.id = id;
			this.view = view;
			this.requestCode = requestCode;
			this.callback = callback;
		}

		/**
		 * @return the view
		 */
		public final View getView() {
			return view;
		}

		/**
		 * @return the id
		 */
		public final String getId() {
			return id;
		}

		public Integer getRequestCode() {
			return requestCode;
		}

		/**
		 * @return the callback
		 */
		public final Callback getCallback() {
			return callback;
		}

		/** {@inheritDoc} */
		@Override
		public String toString() {
			return String.format("View id = %d and Callback = %s", view.getId(), callback);
		}
	}

	/**
	 * @return the _INSTANSE
	 */
	public final Stack<ActivityInfo> getHistory() {
		return history;
	}

	public void startActivity(Intent intent, Callback callback) {
		String id = intent.getComponent().getClassName();
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Logger.i(ActivityGroup.class, "Start activity with id " + id);
		View decor = getLocalActivityManager().startActivity(id, intent).getDecorView();
		// Adds the old one to history
		getHistory().push(new ActivityInfo(id, decor, callback));
		// Changes this Groups View to the new View.
		setContentView(decor);
	}

	public void goBack() {
		if (getHistory().size() > 1) {
			ActivityInfo old = getHistory().pop();
			Callback callback = old.getCallback();
			ActivityInfo act = getHistory().peek();

			Logger.i(ActivityGroup.class, "Start activity with id " + act);
			setContentView(act.getView());

			if (old.getRequestCode() != null) {
				Activity currentActivity = getLocalActivityManager().getCurrentActivity();
				Activity newActivity = getLocalActivityManager().getActivity(act.getId());

				if (currentActivity != null && newActivity != null && newActivity instanceof GenericActivity) {
					int resultCode = currentActivity.getIntent().getIntExtra(GenericActivity.RESULT_CODE,
							RESULT_CANCELED);
					Intent intent = currentActivity.getIntent().getParcelableExtra(GenericActivity.RESULT_DATA);
					((GenericActivity) newActivity).onActivityResult(old.requestCode, resultCode, intent);
				}
			}
			if (callback != null) {
				callback.execute();
			}
		} else {
			finish();
		}
	}

	/**
	 * @see android.app.ActivityGroup#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		if (getHistory().size() > 0) {
			String id = getHistory().peek().getId();
			if (getLocalActivityManager().getActivity(id) instanceof GenericActivity) {
				((GenericActivity) getLocalActivityManager().getActivity(id)).refresh();
			}
		}
	}

	@Override
	public void finish() {
		super.finish();
	}

	/** {@inheritDoc} */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		Logger.d(getClass(), "Cur A " + getCurrentActivity().getClass().getName());

		((GenericActivity) getCurrentActivity()).onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			Logger.d(getClass(), "back button pressed");
			goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void startActivityForResult(Intent intent, int requestCode, Callback callback) {
		String id = intent.getComponent().getClassName();

		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Logger.i(ActivityGroup.class, "Start activity for result with id " + id);
		View decor = getLocalActivityManager().startActivity(id, intent).getDecorView();
		// Adds the old one to history
		getHistory().push(new ActivityInfo(id, decor, requestCode, callback));
		// Changes this Groups View to the new View.
		setContentView(decor);
	}
}
